<?php
include_once dirname(__FILE__)."../../TMS_TEDxCairo_CONFIG/db.php";

class HelperDAO {
	private $host;
	private $userName;
	private $password;
	private $databaseName;
	private $debugMode;
	function __construct($debugMode=false) {
		global $host;
		global $userName;
		global $password;
		global $databaseName;
		
		$this->host = $host;
		$this->userName = $userName;
		$this->password = $password;
		$this->databaseName = $databaseName;
		$this->debugMode = $debugMode;
	}
	/**
	 * simple select finction
	 * @param $sqlQuery  the query to be fire as select
	 * @param $debugMode if true debugging info will be disply on function call
	 */
	public function select($sqlQuery,$params,$scaler=false) {
		if ($this->debugMode) {
			echo "function Fired<br>";
			echo "$this->host, $this->userName, $this->password";
		}
		
		$mysql_connection = mysql_connect ( $this->host, $this->userName, $this->password );
		mysql_set_charset('utf8',$mysql_connection); 
		mysql_select_db ( $this->databaseName, $mysql_connection );
		
		if ($this->debugMode) {
			echo "Database Connection initialized<br>";
		}
		foreach($params as $param){
			$param = mysql_real_escape_string($param);
		}
		$sqlQuery = vsprintf($sqlQuery,$params);
		
		$result = mysql_query ( $sqlQuery );
		if ($this->debugMode) {
			echo "<br>DB-Query : ".$sqlQuery."<br/>";
		}
		$returnData = array ();
		$i = 0;
		$rowCount=mysql_num_rows($result);
		if($rowCount==1 && $scaler)
			$returnData = mysql_fetch_object ( $result );
		else
		while ( $row = mysql_fetch_object ( $result ) ) {
			if ($this->debugMode) {
				echo "$i-<br>";
				print_r ( $row );
				echo "<br>";
			}
			
			$returnData [$i ++] = $row;
		}
		mysql_close ( $mysql_connection );
		if ($this->debugMode) {
			echo "<br>row count:$rowCount<br>The return data formate:<br>";
			print_r ( $returnData );
			echo "<br><br><br>";
			
                        echo "<br>Errors<br>";
                        echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
			echo "<br><br><br>";
		}
		return $returnData;
	}
	/**
	 * Insert Or update Statment
	 * @param $sqlQuery : string
	 * @param $params : array
	 */
	private function genericQuerys($sqlQuery,$params) {
		
		if ($this->debugMode) {
			echo "function Fired<br>";
		}
		
		$mysql_connection = mysql_connect ( $this->host, $this->userName, $this->password );
		mysql_select_db ( $this->databaseName, $mysql_connection );
		
		if ($this->debugMode) {
			echo "Database Connection initialized<br>";
		}
		
		foreach($params as $param)
			$param = mysql_real_escape_string($param);
		
		$sqlQuery = vsprintf($sqlQuery,$params);
		
		if ($this->debugMode) {
			echo $sqlQuery."<br>";
		}
		
		$status = true;
		$result = mysql_query ( $sqlQuery ) or $status = false;
                if($status)
                    $status = mysql_insert_id();
		mysql_close ( $mysql_connection );
		if ($this->debugMode) {
			echo "<br>Status is $status <br>";
                        echo "<br><br><br>";
                        echo "<br>Errors<br>";
                        echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
			echo "<br><br><br>";
		}
		return $status;
	}
	
	public function insert($sqlQuery,$params){
		return $this->genericQuerys($sqlQuery,$params);
	}
	public function update($sqlQuery,$params){
		return $this->genericQuerys($sqlQuery,$params);
	}
	public function delete($sqlQuery,$params){
		return $this->genericQuerys($sqlQuery,$params);
	}
}